package cn.lyjuan.aliyun.mq.conf.java.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 异常时返回稍后重试
 * Created by chad on 2016/10/19.
 */
public abstract class ReconsumeOnErrorListener implements MessageListener
{
    private static Logger log = LogManager.getLogger(ReconsumeOnErrorListener.class.getName());

    @Override
    public Action consume(Message message, ConsumeContext consumeContext)
    {
        try
        {
            return subconsume(message, consumeContext);
        } catch (Throwable t)
        {
            log.error("receive err: tag>>{} key>>{}", message.getTag(), message.getKey(), t);
            return Action.ReconsumeLater;
        }
    }

    protected abstract Action subconsume(Message message, ConsumeContext consumeContext);
}
